<?php


namespace App\Http\Controllers\Auth;


use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Laravel\Passport\ClientRepository;
use Laravel\Passport\Exceptions\OAuthServerException;
use Laravel\Passport\TokenRepository;
use Psr\Http\Message\ServerRequestInterface;


class AuthorizationController extends \Laravel\Passport\Http\Controllers\AuthorizationController
{
	
	/**
	 * Authorize a client to access the user's account.
	 *
	 * @param ServerRequestInterface $psrRequest
	 * @param Request $request
	 * @param ClientRepository $clients
	 * @param TokenRepository $tokens
	 * @return Response
	 * @throws OAuthServerException
	 */
	public function authorize(ServerRequestInterface $psrRequest,
		Request $request,
		ClientRepository $clients,
		TokenRepository $tokens)
	{
		$authRequest = $this->withErrorHandling(function () use ($psrRequest) {
			return $this->server->validateAuthorizationRequest($psrRequest);
		});

		$scopes = $this->parseScopes($authRequest);

		$token = $tokens->findValidToken(
			$user = $request->user(),
			$client = $clients->find($authRequest->getClient()->getIdentifier())
		);

		if (($token && $token->scopes === collect($scopes)->pluck('id')->all()) ||
			$client->skipsAuthorization()) {
			return $this->approveRequest($authRequest, $user);
		}

		$request->session()->put('authRequest', $authRequest);

		return $this->response->view('auth.passwords.authorize', [
			'client' => $client,
			'user' => $user,
			'scopes' => $scopes,
			'request' => $request,
		]);
	}



	
}